home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7515 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Simple Program Question
  5. Date: 26 Feb 1996 20:05:53 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Message-ID: <4gtou1$d3q@solutions.solon.com>
  8. References: <4gsr9u$sk6@newsbf02.news.aol.com> <4gti5g$d78@garden.csc.calpoly.edu>
  9. NNTP-Posting-Host: solutions.solon.com
  10.  
  11. In article <4gti5g$d78@garden.csc.calpoly.edu>,
  12. Dan Stubbs <dstubbs@garden.csc.calpoly.edu> wrote:
  13. >>long int i, j, k, l;
  14. >>long int count;
  15.  
  16. >>int
  17. >>main (void)
  18. >>{
  19. >>    do
  20. >>    {
  21. >>        (l = i + j + k);
  22. >>        (i = 1);
  23. >>        (j = 2);
  24. >>        (k = 3);
  25. >>        (i++, j++, k++);
  26. >>        (++count);
  27. >>    if (count % 1000000 == 0)
  28. >>     {
  29. >>        printf("%ld(i) + %ld(j) + %ld(k) = %ld(l)", i, j, k, l);
  30. >>     }
  31. >>
  32. >>    } while (0 < i < j < k < l);
  33.  
  34. >>    return (0);
  35. >>}
  36.  
  37. >It appears to me that you are to fix el (use el instead of l) and then find 
  38. >i, j, and k such that 0<i<j<k and i+j+k = el. Your code doesn't do anything
  39. >like that. A couple of problems: the first time l = i+j+k is encountered
  40. >i, j, and k have had no value assigned and hence what happens is undefined.
  41.  
  42. Bullshit; globals are auto-initialized to zero.  :)
  43.  
  44. >Perhaps that is where your program dies.
  45.  
  46. More likely, it's that the first time through, count is set to 1, so nothing
  47. is printed.  Then
  48.     0 < i < j < k < l
  49. is evaluated, and turns out to be false, because < is a binary op, not a
  50. normal math relational op, and the program terminates.
  51.  
  52. Nah.  Too easy.
  53.  
  54. >Then, each time through your do-while loop i,j and k are *all* incremented by
  55. >exactly 1. Hence the value of i,j and k in ;your program are 1,2,3; 2,3,4;
  56. >3,4,5; 4,5,6; ...  j,j+1,j+2, ... . Surely you want something more general
  57. >than that.
  58.  
  59. I would agree; the original program is naive.
  60.  
  61. >I don't believe your code could compile. Your while condition:
  62.  
  63. >   while (0<i<j<k<l) is not legal. I suspect you want something like:
  64.  
  65. >   while (0<i && i<j && j<k && k<l). 
  66.  
  67. He probably does; however, his code is *legal*.  Just stupid.
  68.  
  69. >Your logic is pretty far off, so here is a solution to the problem as I
  70. >understand it. (The fixed value of el is read from the command line.)
  71.  
  72. [Solution snipped.  The only immediate problem I saw was that it didn't
  73.  return a value.]
  74.  
  75. -s
  76. -- 
  77. Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
  78. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  79. FUCK the communications decency act.  Goddamned government.  [literally.]
  80. The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
  81.